import {
  getIndustryList,
  getIndustryPositionList,
  getCompanyDepartmentList,
  searchCompanyDepartmentList
} from "@/services/global";
const GlobalModel = {
  namespace: "global",
  state: {
    collapsed: false,
    notices: [],
    industryList: [],
    industryPositionList: [],
    companyDepartment: {},
    companyDepartmentList: []
  },
  effects: {
    *getIndustryList(_, { call, put, select }) {
      const response = yield call(getIndustryList);
      if (!response) return;
      yield put({
        type: "saveState",
        payload: { industryList: response.data }
      });
    },

    *getIndustryPositionList({ payload }, { call, put, select }) {
      const response = yield call(getIndustryPositionList, payload);
      if (!response) return;
      yield put({
        type: "saveState",
        payload: { industryPositionList: response.data }
      });
    },

    *getCompanyDepartmentList({ payload }, { call, put, select }) {
      const response = yield call(getCompanyDepartmentList, payload);
      if (!response) return;
      yield put({
        type: "saveState",
        payload: { companyDepartmentList: response.data }
      });
    },

    *searchCompanyDepartmentList({ payload }, { call, put, select }) {
      const response = yield call(searchCompanyDepartmentList, payload);
      if (!response) return;
      yield put({
        type: "saveState",
        payload: { companyDepartment: response.data }
      });
    }
  },
  reducers: {
    saveState(state, action) {
      return {
        ...state,
        ...action.payload
      };
    },
    changeLayoutCollapsed(
      state = {
        notices: [],
        collapsed: true
      },
      { payload }
    ) {
      return { ...state, collapsed: payload };
    },

    saveNotices(state, { payload }) {
      return {
        collapsed: false,
        ...state,
        notices: payload
      };
    },

    saveClearedNotices(
      state = {
        notices: [],
        collapsed: true
      },
      { payload }
    ) {
      return {
        collapsed: false,
        ...state,
        notices: state.notices.filter(item => item.type !== payload)
      };
    }
  },
  subscriptions: {
    setup({ history }) {
      // Subscribe history(url) change, trigger `load` action if pathname is `/`
      history.listen(({ pathname, search }) => {
        if (typeof window.ga !== "undefined") {
          window.ga("send", "pageview", pathname + search);
        }
      });
    }
  }
};
export default GlobalModel;
